'use strict'
const SUN = require('../base/sun')
const idsDao = require('./idsDao')
const db = require('./db')

class UserDao {
    constructor() {

    }
    newAndSave(username, password, cb) {
        let self = this
        idsDao.findOneAndUpdate('userId', function(err, numAffected, affectedDocuments) {
            if (err || !affectedDocuments)
                throw err
            let id = affectedDocuments.userId * 1000 + 1 //id后面3位是服务器id
            console.log('id',id)
            db.users.insert({
                _id: id,
                username: username,
                password: password,
                registerTime: new Date().getTime()
            }, cb)
        })
    }
    getUserByName(username, cb) {
        db.users.findOne({
            username: username
        }, function(err, doc) {
            cb(err, doc)
        })
    }
    getUserByQuery(query, cb) {
        db.users.findOne(query, function(err, doc) {
            cb(err, doc)
        })
    }
}
let userDao = new UserDao()
module.exports = userDao
